package com.cong;


import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.LikeTable;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;

import java.util.Collections;

/**
 * 代码生成器
 */
public class BaseGeneratorForMysql {

    // 数据源配置
    private static final DataSourceConfig DATA_SOURCE_CONFIG  =  new DataSourceConfig
            .Builder("jdbc:mysql://127.0.0.1:3306/admin-base?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true", "root", "my123sql")
            .schema("admin-base")
            .typeConvert(new MySqlTypeConvert() {
                @Override
                public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
                    System.out.println("转换类型：" + fieldType);
                    if (fieldType.toLowerCase().contains("datetime")) {
                        return DbColumnType.DATE;
                    }
                    if (fieldType.toLowerCase().contains("tinyint")) {
                        return DbColumnType.INTEGER;
                    }
                    if (fieldType.toLowerCase().contains("int")) {
                        return DbColumnType.INTEGER;
                    }
                    return super.processTypeConvert(globalConfig, fieldType);
                }
            })
            .build();



    /**
     * 策略配置
     */
    protected static StrategyConfig.Builder strategyConfig() {
        return new StrategyConfig.Builder().likeTable(new LikeTable("meeting_category"));
    }

    /**
     * 全局配置
     */
    protected static GlobalConfig.Builder globalConfig() {
        return new GlobalConfig.Builder()
                .outputDir(System.getProperty("user.dir") + "/src/main/java")
                .dateType(DateType.TIME_PACK)
                .enableSwagger();
    }

    /**
     * 添加 PackageConfig 配置
     * @return
     */
    protected static PackageConfig.Builder packageConfig() {
        return new PackageConfig.Builder()
                .parent("") // 设置父包名
                .entity("com.cong.modules.meeting.model")      // 设置实体类包名
                .mapper("com.cong.modules.meeting.mapper")      // 设置 Mapper 接口包名
                .service("com.cong.modules.meeting.service")    // 设置 Service 接口包名
                .serviceImpl("com.cong.modules.meeting.service.impl") // 设置 Service 实现类包名
                .controller("com.cong.modules.meeting.controller") // 设置 Controller 包名
                .pathInfo(new PackageConfig.Builder().build().getPathInfo())
                // 添加以下代码以指定 Mapper XML 文件的位置
                .pathInfo(Collections.singletonMap(OutputFile.xml, "src/main/resources/mapper/meeting"));
    }

    public static void main(String[] args) {
        AutoGenerator generator = new AutoGenerator(DATA_SOURCE_CONFIG);
        generator.strategy(strategyConfig().build());
        generator.global(globalConfig().build());
        generator.packageInfo(packageConfig().build());
        generator.execute();
    }
}
